package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.ObjectEncoding;
	import flash.net.Socket;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	
	public class test extends Sprite {

		private var net:Socket;
		private var btn:TextField = new TextField;
		
		public function test() {
			trace("jw create project")
			net = new Socket();
			net.endian = Endian.LITTLE_ENDIAN;
			net.addEventListener(Event.CONNECT, onConnected);
			net.addEventListener(Event.CLOSE, onSocketClose);
			net.addEventListener(IOErrorEvent.IO_ERROR, onSocketIOError);
			net.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSocketSecurityError);
			net.addEventListener(ProgressEvent.SOCKET_DATA, onRecvData);
			net.connect("127.0.0.1",4700);
			
			btn.text = "dd";
			btn.addEventListener(MouseEvent.CLICK,clickHandler);
		}
		
		private function onConnected(e:Event):void{
			trace("onConnected")
			addChild(btn);
		}
		
		private function onSocketClose(e:Event):void{
			trace("onSocketClose")
		}
		
		private function onSocketIOError(e:Event):void{
			trace("onSocketIOError")
		}
		
		private function onSocketSecurityError(e:Event):void{
			trace("onSocketSecurityError")
		}
		
		private function onRecvData(e:ProgressEvent):void{
			if( net.bytesAvailable < 4 ) {
				return;
			}
			var bytes:ByteArray = new ByteArray();
			bytes.endian = Endian.LITTLE_ENDIAN;
			net.readBytes(bytes);
			while(bytes.bytesAvailable >0){
				trace(bytes.readByte());
			}
		}
		
		private function clickHandler(e:Event):void{
			net.writeBoolean(true);
			net.writeByte(256);
			
			net.writeInt(9);
			net.writeInt(7);
			net.writeInt(2);
			net.writeInt(3);
			net.writeInt(3);
			net.writeInt(4);
			net.flush();
		}
	}
}